﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace web.admin
{
    public partial class DiseaseTypeEdit : System.Web.UI.Page
    {
        private DiseaseBLL diseaseBll = DiseaseBLL.Instance;
        private string URL_BACK = "DiseaseType.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            try
            {
                int id = RequestTools.GetQueryInt("id", -1);
                if (id > 0)
                {
                    DiseaseTypeInfo info = diseaseBll.GetDiseaseType(id);
                    IList list = new ArrayList();
                    list.Add(info);

                    this.diseaseTypeInfo.DataSource = list;
                    this.diseaseTypeInfo.DataBind();
                }
            }
            catch
            {
                //重定向到出错页面
            }
        }

        protected void UpdateButton_click(object sender, EventArgs e)
        {
            //数据有效性检查
            try
            {
                DiseaseTypeInfo info = validateForm(this.diseaseTypeInfo);
                if (info != null)
                {
                    info.Id = Convert.ToInt32(this.diseaseTypeInfo.DataKey.Value);

                    //准备数据,调用SqlServerDAL执行更新
                    if (diseaseBll.UpdateDiseaseType(info))  //成功
                    {
                        HttpContext.Current.Response.Redirect(URL_BACK, true);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect(URL_BACK, true);
        }

        //数据有效性检查
        private DiseaseTypeInfo validateForm(DetailsView formViewCtl)
        {
            DiseaseTypeInfo info = new DiseaseTypeInfo();

            info.Name = ((TextBox)formViewCtl.FindControl("name")).Text;
 
            return info;
        }
    }
}
